package com.hfxt.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfxt.entity.Bill;
import com.hfxt.entity.Provider;
import com.hfxt.entity.User;
import com.hfxt.mapper.BillMapper;
import com.hfxt.mapper.ProviderMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("providerService")
public class ProviderServiceImpl implements ProviderService {
    @Resource
    private ProviderMapper providerMapper;
    @Resource
    private BillMapper billMapper;

    @Override
    public PageInfo<Provider> queryAllProviderByPageInfo(String proName, Integer pageNum, Integer pageSize) {
        System.out.println("pageNum = " + pageNum);
        System.out.println("pageSize = " + pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<Provider> providerList = providerMapper.queryAllProvidersByCondition(proName,pageNum,pageSize);
        return new PageInfo<Provider>(providerList);
    }

    @Override
    public List<Provider> queryAllProvidersByCondition(String proName, Integer pageIndex, Integer pageSize) {
        return providerMapper.queryAllProvidersByCondition(proName, (pageIndex - 1) * pageSize, pageSize);
    }

    @Override
    public int queryCountByCondition(String proName) {
        return providerMapper.queryCountByCondition(proName);
    }

    @Override
    public Provider queryProviderById(Long id) {
        return providerMapper.queryProviderById(id);
    }

    @Override
    public int deleteProviderById(Long id) {
        System.out.println("service层-----deleteProviderById-----id" + id);
        List<Bill> billList = billMapper.queryBillsByProviderId(id);
        System.out.println("billList是否为空集合------" + billList.isEmpty());
        if (!billList.isEmpty()) {
            //存在未付款的订单，不能执行删除供应商操作
            System.out.println("该供应商存在未付款的订单，不能删除该供应商！");
            return 2;
        } else {
            //该供应商不存在未付款订单
            //查找该供应商下是否有订单
            List<Bill> billList1 = billMapper.queryBillsByProviderId(id);
            if (billList1 != null) {
                billMapper.deleteBillsByProviderId(id);
            }
            return providerMapper.deleteProviderById(id);

        }
    }

    @Override
    public int updateProvider(Provider provider) {
        return providerMapper.updateProvider(provider);
    }

    @Override
    public int addProvider(Provider provider) {
        return providerMapper.addProvider(provider);
    }

    @Override
    public List<Provider> queryAllProviders() {
        return providerMapper.queryAllProviders();
    }

    @Override
    public Provider queryProviderByProCode(String proCode) {
        return providerMapper.queryProviderByProCode(proCode);
    }
}
